package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.Base64;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface Base64Dao {

    /**
     * 根据 type + outer_id  查询
     */
    @Results(id = "base64ResultMap", value = {
            @Result(column = "base64_id", property = "base64Id", jdbcType = JdbcType.INTEGER),
            @Result(column = "base64", property = "base64", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "type", property = "type", jdbcType = JdbcType.INTEGER),
            @Result(column = "outer_id", property = "outerId", jdbcType = JdbcType.INTEGER),
    })
    @Select("select * from " + DB1TableName.BASE64 + " where type = #{type} and outer_id = #{outerId}")
    List<Base64> getBase64ByTypeAndOuterId(@Param("type") Integer type, @Param("outerId") Integer outerId);

    /**
     * 新增
     */
    @Insert("insert into " + DB1TableName.BASE64 + "(base64,type,outer_id,create_time) values " +
            "(#{base64.base64},#{base64.type},#{base64.outerId},#{base64.createTime})")
    Integer addBase64(@Param("base64") Base64 base64);

    /**
     * 更新
     */
    @Update("<script>" +
            "update " + DB1TableName.BASE64 +
            "<set>" +
            "<if test='base64.base64 != null and base64.base64 != \"\" '>" +
            " base64 = #{base64.base64}," +
            "</if>" +
            "<if test='base64.outerId != null and base64.outerId != \"\" '>" +
            " outer_id = #{base64.outerId}" +
            "</if>" +
            "</set>" +
            " where base64_id = #{base64.base64Id}" +
            "</script>")
    Integer updateBase64(@Param("base64") Base64 base64);
}
